package goods

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/goodSpec"
	"github.com/flipped-aurora/gin-vue-admin/server/model/goodSpecValue"
	model "github.com/flipped-aurora/gin-vue-admin/server/model/goodSpecValueDetail"
	"github.com/flipped-aurora/gin-vue-admin/server/model/goods"
	goodsReq "github.com/flipped-aurora/gin-vue-admin/server/model/goods/request"
	"gorm.io/gorm"
)

type LsGoodsService struct{}

// CreateLsGoods 创建lsGoods表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodsService *LsGoodsService) CreateLsGoods(lsGoodsReq *goodsReq.GoodInfoRequest) (err error) {
	lsGoods := &goods.LsGoods{
		GoodType:              lsGoodsReq.GoodType,
		GoodName:              lsGoodsReq.GoodName,
		VideoEnable:           lsGoodsReq.VideoEnable,
		VideoSource:           lsGoodsReq.VideoSource,
		VideoUrl:              lsGoodsReq.VideoUrl,
		VideoCover:            lsGoodsReq.VideoCover,
		GoodBrandId:           lsGoodsReq.GoodBrandId,
		GoodUnit:              lsGoodsReq.GoodUnit,
		GoodSupplier:          lsGoodsReq.GoodSupplier,
		GoodSpec:              lsGoodsReq.GoodSpec,
		Logistics:             lsGoodsReq.Logistics,
		ShippingFee:           lsGoodsReq.ShippingFee,
		ShippingFeeTemplement: lsGoodsReq.ShippingFeeTemplement,
		GoodDetail:            lsGoodsReq.GoodDetail,
		StockLimit:            lsGoodsReq.StockLimit,
		VirtualSale:           lsGoodsReq.VirtualSale,
		VirtualPageView:       lsGoodsReq.VirtualPageView,
		ServiceGuarantee:      lsGoodsReq.ServiceGuarantee,
		QuotaCode:             lsGoodsReq.QuotaCode,
		QuotaCount:            lsGoodsReq.QuotaCount,
		GoodStatus:            lsGoodsReq.GoodStatus,
	}
	err = global.GVA_DB.Create(lsGoods).Error
	for _, spec := range lsGoodsReq.MenuBtn {
		lsGoodSpec := &goodSpec.GoodSpec{
			Spec:   spec.GoodSpecName,
			GoodId: lsGoods.ID,
		}
		err = global.GVA_DB.Create(lsGoodSpec).Error
		lsGoodSpecValueDetails := &model.GoodSpecValueDetail{
			GoodID:        int(lsGoods.ID),
			GoodSpecValue: spec.GoodSpecValue,
			GoodSpecImage: spec.GoodSpecImage,
			GoodSpecPrice: spec.GoodSpecPrice,
			MarketPrice:   spec.MarketPrice,
			CostPrice:     spec.CostPrice,
			Stock:         spec.Stock,
			Volume:        spec.Volume,
			Weight:        spec.Weight,
			BarCode:       spec.BarCode,
			GoodSpecName:  spec.GoodSpecName,
		}
		err = global.GVA_DB.Create(lsGoodSpecValueDetails).Error
		lsGoodSpecValue := &goodSpecValue.GoodSpecValue{
			SpecId: lsGoodSpec.ID,
			GoodId: lsGoods.ID,
			Value:  lsGoodSpecValueDetails.ID,
		}
		err = global.GVA_DB.Create(lsGoodSpecValue).Error
	}
	return err
}

// DeleteLsGoods 删除lsGoods表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodsService *LsGoodsService) DeleteLsGoods(ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&goods.LsGoods{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&goods.LsGoods{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteLsGoodsByIds 批量删除lsGoods表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodsService *LsGoodsService) DeleteLsGoodsByIds(IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&goods.LsGoods{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&goods.LsGoods{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateLsGoods 更新lsGoods表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodsService *LsGoodsService) UpdateLsGoods(lsGoods goods.LsGoods) (err error) {
	err = global.GVA_DB.Model(&goods.LsGoods{}).Where("id = ?", lsGoods.ID).Updates(&lsGoods).Error
	return err
}

// GetLsGoods 根据ID获取lsGoods表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodsService *LsGoodsService) GetLsGoods(ID string) (lsGoods goods.LsGoods, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&lsGoods).Error
	return
}

// GetLsGoodsInfoList 分页获取lsGoods表记录
// Author [yourname](https://github.com/yourname)
func (lsGoodsService *LsGoodsService) GetLsGoodsInfoList(info goodsReq.LsGoodsSearch) (list []goods.LsGoods, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&goods.LsGoods{})
	var lsGoodss []goods.LsGoods
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.GoodType != nil && *info.GoodType != "" {
		db = db.Where("good_type = ?", *info.GoodType)
	}
	if info.GoodName != nil && *info.GoodName != "" {
		db = db.Where("good_name LIKE ?", "%"+*info.GoodName+"%")
	}
	if info.VideoEnable != nil {
		db = db.Where("video_enable = ?", *info.VideoEnable)
	}
	if info.VideoSource != nil && *info.VideoSource != "" {
		db = db.Where("video_source = ?", *info.VideoSource)
	}
	if info.GoodBrandId != nil {
		db = db.Where("good_brand_id = ?", *info.GoodBrandId)
	}
	if info.GoodUnit != nil && *info.GoodUnit != "" {
		db = db.Where("good_unit = ?", *info.GoodUnit)
	}
	if info.GoodSupplier != nil {
		db = db.Where("good_supplier = ?", *info.GoodSupplier)
	}
	if info.GoodSpec != nil && *info.GoodSpec != "" {
		db = db.Where("good_spec = ?", *info.GoodSpec)
	}
	if info.Logistics != nil && *info.Logistics != "" {
		db = db.Where("logistics = ?", *info.Logistics)
	}
	if info.ShippingFee != nil && *info.ShippingFee != "" {
		db = db.Where("shipping_fee = ?", *info.ShippingFee)
	}
	if info.ServiceGuarantee != nil && *info.ServiceGuarantee != "" {
		db = db.Where("service_guarantee = ?", *info.ServiceGuarantee)
	}
	if info.GoodStatus != nil && *info.GoodStatus != "" {
		db = db.Where("good_status = ?", *info.GoodStatus)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&lsGoodss).Error
	return lsGoodss, total, err
}
func (lsGoodsService *LsGoodsService) GetLsGoodsPublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
